<?php
/**
 * Created by PhpStorm.
 * User: zy-l
 * Date: 2019/6/19
 * Time: 1:23 AM
 */

namespace App\HttpController\Admin;

use App\Common\Exception\ThrowException;
use App\HttpController\AdminBase;
use App\Model\InfoMessage;
use App\Model\InfoSetting;
use EasySwoole\Validate\Validate;

/**
 * 联系管理
 * Class Adv
 * @package App\HttpController\Admin
 */
class Contactus extends AdminBase
{
    /***
     * 联系我们设置模型
     * @return InfoSetting
     */
    public function getInfoSettingModel()
    {
        return new InfoSetting();
    }

    /**
     * @return InfoMessage
     */
    public function getInfoMessageModel()
    {
        return new InfoMessage();
    }

    /**
     * 案例列表
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function index()
    {
        if ($this->isPost()) {
            $where=[];
            $where['id|order']='desc';
            return $this->writePageJson('获取列表数据', $this->getInfoMessageModel()->getPageAll($this->getInfoMessageModel()->analysisWhere($where),$this->postParam('page')));
        }
        return $this->fetch('index');
    }


    /**
     * 修改状态
     */
    public function change_status(){
        return $this->writeJson('操作成功',$this->getInfoMessageModel()->changeStatus($this->postParams()));
    }

    /**
     * 设置列表
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function setting()
    {
        if ($this->isPost()) {
            $where=[];
            $title = $this->postParam("title");
            if (!empty($title)) {
                $where["title|like"] = '%' . $title . '%';
            }

            return $this->writePageJson('获取列表数据', $this->getInfoSettingModel()->getPageAll($this->getInfoSettingModel()->analysisWhere($where),$this->postParam('page')));
        }
        return $this->fetch('setting');
    }

    /**
     * 新增
     * @return string|void
     */
    public function setting_add()
    {
        if($this->isPost()){
            if($this->getInfoSettingModel()->findOne(['type'=>$this->postParam('type')])){
                throw  new ThrowException(ThrowException::EROOR_DATA_EXIT_ROW);
            }
            return $this->writeJson('操作成功',$this->getInfoSettingModel()->updateOrCreateData($this->postParams(),['id'=>$this->postParam('id')]));
        }
        return $this->fetch('setting_add');
    }

    /**
     * 设置编辑
     * @return string|void
     */
    public function setting_edit()
    {
        if($this->isPost()){
            return $this->writeJson('操作成功',$this->getInfoSettingModel()->updateOrCreateData($this->postParams(),['id'=>$this->postParam('id')]));
        }
        $info=$this->getInfoSettingModel()->findOne(['id'=>$this->getParam('id')]);
        $this->assign(['info'=>$info]);
        return $this->fetch('setting_edit');
    }


    /**
     * 设置数据删除
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function setting_del(){
        return $this->writeJson('删除成功',$this->getInfoSettingModel()->delData(['id'=>$this->postParam('id')]));
    }

    public function del(){
        return $this->writeJson('删除成功',$this->getInfoMessageModel()->delData(['id'=>$this->postParam('id')]));
    }
    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }
}